package com.mcxx.modules.temporary.order.service.write;

import com.mcxx.common.exception.BizException;
import com.mcxx.convert.OrderConvert;
import com.mcxx.modules.miniSalvation.order.entity.OrderDetailEntity;
import com.mcxx.modules.temporary.order.dao.write.TmpOrderCertWriteDao;
import com.mcxx.modules.temporary.order.dto.TmpAuditDTO;
import com.mcxx.modules.temporary.order.entity.TmpOrderDetailEntity;
import com.mcxx.modules.temporary.order.dao.write.TmpOrderDetailWriteDao;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;

@Service
@TaTransactional
public class TmpOrderDetailWriteService {

    @Autowired
    private TmpOrderDetailWriteDao tmpOrderDetailWriteDao;
    @Autowired
    private TmpOrderCertWriteDao tmpOrderCertWriteDao;




    public int update(TmpOrderDetailEntity tmpOrderDetail) {
        int rst = tmpOrderDetailWriteDao.update(tmpOrderDetail);
        if(rst <= 0){

            throw new BizException(FailureCode.ERR_9999);
        }
    	return rst;
    }


    public int updateIgnoreNull(TmpOrderDetailEntity tmpOrderDetail) {
    	return tmpOrderDetailWriteDao.updateIgnoreNull(tmpOrderDetail);
    }


    public int delete(TmpOrderDetailEntity tmpOrderDetail) {
    	return tmpOrderDetailWriteDao.delete(tmpOrderDetail);
    }

    public void saveFirstAuditOrderDetail(TmpAuditDTO tmpAuditDTO, UserAccountVo userAccountVo){
        TmpOrderDetailEntity orderDetailEntity = OrderConvert.convertOrderDetailEntity(tmpAuditDTO, userAccountVo,Constant.CheckDatailStatus.FIRST_he);

        orderDetailEntity.setBusinessStatus(Constant.RemarkOperType.STREET);
        orderDetailEntity.setCheckType(Constant.CheckDatailStatus.FIRST_he);

        this.saveOrderDetail(orderDetailEntity, userAccountVo);
    }


    public void saveAuditOrderDetail(TmpAuditDTO tmpAuditDTO, UserAccountVo userAccountVo){
        TmpOrderDetailEntity orderDetailEntity = OrderConvert.convertOrderDetailEntity(tmpAuditDTO, userAccountVo,Constant.CheckDatailStatus.he);

        orderDetailEntity.setBusinessStatus(Constant.RemarkOperType.STREET);
        orderDetailEntity.setCheckType(Constant.CheckDatailStatus.he);

        this.saveOrderDetail(orderDetailEntity, userAccountVo);
    }


    public void saveApprovalOrderDetail(@RequestBody TmpOrderDetailEntity orderDetailEntity, UserAccountVo userAccountVo){
        orderDetailEntity.setBusinessStatus(Constant.RemarkOperType.COUNTY);
        orderDetailEntity.setCheckType(Constant.CheckDatailStatus.Pi);

        this.saveOrderDetail(orderDetailEntity, userAccountVo);

        if(CollectionUtils.isNotEmpty(orderDetailEntity.getCertIdList())){
            int rst = tmpOrderCertWriteDao.updateById(orderDetailEntity.getCertIdList(), orderDetailEntity.getOrderId(), "1", orderDetailEntity.getFamilyId());
            if(rst <= 0){
                throw new BizException(FailureCode.ERR_9999);
            }
        }
    }


    private void saveOrderDetail(TmpOrderDetailEntity orderDetailEntity, UserAccountVo userAccountVo){
        orderDetailEntity.setCheckDate(new Date());
        orderDetailEntity.setCheckPeople(userAccountVo.getUsername());
        orderDetailEntity.setOperator(userAccountVo.getLoginId());
        orderDetailEntity.setOrgId(userAccountVo.getOrgId());

        int rst = 0;
        if(CommonUtil.invalidArgs(orderDetailEntity.getId())){
            orderDetailEntity.setId(Constant.getUUID());
            orderDetailEntity.setCreateBy(userAccountVo.getUserId());
            orderDetailEntity.setCreateTime(new Date());
            rst = tmpOrderDetailWriteDao.insertIgnoreNull(orderDetailEntity);
        }else{
            rst = tmpOrderDetailWriteDao.updateIgnoreNull(orderDetailEntity);
        }
        if(rst <= 0){
            throw new BizException(FailureCode.ERR_9999);
        }
    }

}
